library(dplyr)

###############
# decomposing inefficiency

summary_stats<- read.csv("file:///C:/Users/bjb210/OneDrive - University of Exeter/lab games/analysis/monitoring_homogenous_lab_game_analysis_R/181219_All_data_lab_games_stata.csv",
                         header=TRUE, sep=",")


group_outcomes <- summary_stats %>% 
  group_by(Type, Setting, Period, Group_w_in_treat) %>%  
  summarise(if.mon.twentyfive = sum(if.mon.twentyfive),
            if.mon.ninety = sum(if.mon.ninety),
            mean_dn = mean(DN_applied),
            num_mon = sum(Monitoring_decision),
            ) %>%
  as.data.frame()
group_outcomes

group_outcomes$max_eff<-30.3
# this is the maximum possible b to soc

group_outcomes$tot_ineff_25<-group_outcomes$max_eff - group_outcomes$if.mon.twentyfive
group_outcomes$tot_ineff_90<-group_outcomes$max_eff - group_outcomes$if.mon.ninety
# difference between realised and max is total inefficiency

# then we decompose this

group_outcomes$tot_ineff_mon_costs_25<-0.25*group_outcomes$num_mon
group_outcomes$tot_ineff_mon_costs_90<-0.90*group_outcomes$num_mon
# the cost owing to monitoring is straightforward

# costs owing to each of the others is then somewhat harder
# for too high aggregate pollution, it is maximum efficiency minus the efficiency from had the same total pollution
# been equally split

# and for the loss owing to allocative inefficiency, it is what would have been achieved with an even allocation minus
# that which was actually achieved

group_outcomes$eff_at_mean<-6*(8.8-0.0004*((100 - 5*group_outcomes$mean_dn)^2))-6*0.3*group_outcomes$mean_dn
# first calculate the efficiency at the mean (ignoring the impact of monitoring decisions)

group_outcomes$eff_ignore_m_cost<-group_outcomes$if.mon.twentyfive+0.25*group_outcomes$num_mon
# then need to calculate what the benefits are to society as if no monitoring happened
# not because this is as if monitoring doesnt occur, it doesnt matter how we get to that - 
# from inclusion with m=.25 or inclusion with m=.90 both deliver the same number

group_outcomes$tot_ineff_too_much <- group_outcomes$max_eff - group_outcomes$eff_at_mean
group_outcomes$tot_ineff_unequal <- group_outcomes$eff_at_mean - group_outcomes$eff_ignore_m_cost

group_outcomes$check_0_25 <- group_outcomes$tot_ineff_mon_costs_25 + group_outcomes$tot_ineff_too_much + group_outcomes$tot_ineff_unequal - group_outcomes$tot_ineff_25
max(group_outcomes$check_0_25)

group_outcomes$check_0_90 <- group_outcomes$tot_ineff_mon_costs_90 + group_outcomes$tot_ineff_too_much + group_outcomes$tot_ineff_unequal - group_outcomes$tot_ineff_90
max(group_outcomes$check_0_90)


group_means_ineff_25 <- group_outcomes %>% 
  group_by(Type, Setting) %>%  
  summarise(tot_eff = mean(if.mon.twentyfive),
            tot_ineff_25 = mean(tot_ineff_25)/30.3*100,
            tot_ineff_mon_costs_25 = mean(tot_ineff_mon_costs_25)/30.3*100,
            tot_ineff_too_much = mean(tot_ineff_too_much)/30.3*100,
            tot_ineff_unequal = mean(tot_ineff_unequal)/30.3*100
            ) %>%
  as.data.frame()
group_means_ineff_25

group_means_ineff_90 <- group_outcomes %>% 
  group_by(Type, Setting) %>%  
  summarise(tot_eff = mean(if.mon.ninety),
            tot_ineff_90 = mean(tot_ineff_90)/30.3*100,
            tot_ineff_mon_costs_90 = mean(tot_ineff_mon_costs_90)/30.3*100,
            tot_ineff_too_much = mean(tot_ineff_too_much)/30.3*100,
            tot_ineff_unequal = mean(tot_ineff_unequal)/30.3*100
  ) %>%
  as.data.frame()
group_means_ineff_90


ineff_pigou_25<-(0.25*6)/30.3*100
ineff_pigou_90<-(0.90*6)/30.3*100

